<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>高级AI推荐系统 - 外卖平台</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        
        body {
            font-family: 'Microsoft YaHei', Arial, sans-serif;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            color: #333;
        }
        
        .container {
            max-width: 1400px;
            margin: 0 auto;
            padding: 20px;
        }
        
        .header {
            background: rgba(255, 255, 255, 0.95);
            backdrop-filter: blur(20px);
            border-radius: 20px;
            padding: 30px;
            margin-bottom: 30px;
            box-shadow: 0 25px 50px rgba(0,0,0,0.15);
            text-align: center;
        }
        
        .header h1 {
            font-size: 3rem;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            background-clip: text;
            margin-bottom: 10px;
        }
        
        .header p {
            color: #666;
            font-size: 1.2rem;
        }
        
        .ai-dashboard {
            display: grid;
            grid-template-columns: 1fr 1fr;
            gap: 30px;
            margin-bottom: 30px;
        }
        
        .dashboard-panel {
            background: rgba(255, 255, 255, 0.95);
            backdrop-filter: blur(20px);
            border-radius: 20px;
            padding: 30px;
            box-shadow: 0 25px 50px rgba(0,0,0,0.15);
        }
        
        .panel-title {
            font-size: 1.5rem;
            font-weight: bold;
            margin-bottom: 20px;
            display: flex;
            align-items: center;
            gap: 10px;
        }
        
        .ai-controls {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
            gap: 15px;
            margin-bottom: 20px;
        }
        
        .control-group {
            background: #f8f9fa;
            padding: 20px;
            border-radius: 12px;
            border: 1px solid #e9ecef;
        }
        
        .control-label {
            font-weight: 600;
            margin-bottom: 10px;
            color: #333;
        }
        
        .control-input {
            width: 100%;
            padding: 12px;
            border: 2px solid #e9ecef;
            border-radius: 8px;
            font-size: 14px;
            transition: border-color 0.3s ease;
        }
        
        .control-input:focus {
            outline: none;
            border-color: #667eea;
        }
        
        .control-select {
            width: 100%;
            padding: 12px;
            border: 2px solid #e9ecef;
            border-radius: 8px;
            font-size: 14px;
            background: white;
        }
        
        .control-range {
            width: 100%;
            margin: 10px 0;
        }
        
        .btn {
            padding: 12px 24px;
            border: none;
            border-radius: 8px;
            cursor: pointer;
            font-weight: 600;
            transition: all 0.3s ease;
            text-decoration: none;
            display: inline-block;
            margin: 5px;
        }
        
        .btn-primary {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
        }
        
        .btn-primary:hover {
            transform: translateY(-2px);
            box-shadow: 0 5px 15px rgba(102, 126, 234, 0.4);
        }
        
        .btn-success {
            background: #28a745;
            color: white;
        }
        
        .btn-info {
            background: #17a2b8;
            color: white;
        }
        
        .btn-warning {
            background: #ffc107;
            color: #333;
        }
        
        .btn-danger {
            background: #dc3545;
            color: white;
        }
        
        .recommendation-results {
            background: rgba(255, 255, 255, 0.95);
            backdrop-filter: blur(20px);
            border-radius: 20px;
            padding: 30px;
            box-shadow: 0 25px 50px rgba(0,0,0,0.15);
            margin-bottom: 30px;
        }
        
        .recommendation-grid {
            display: grid;
            grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
            gap: 20px;
            margin-top: 20px;
        }
        
        .recommendation-card {
            background: white;
            border-radius: 12px;
            padding: 20px;
            box-shadow: 0 4px 15px rgba(0,0,0,0.1);
            transition: transform 0.3s ease;
            position: relative;
        }
        
        .recommendation-card:hover {
            transform: translateY(-5px);
        }
        
        .card-header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 15px;
        }
        
        .card-title {
            font-size: 1.2rem;
            font-weight: bold;
            color: #333;
        }
        
        .ai-score {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            padding: 5px 12px;
            border-radius: 20px;
            font-size: 12px;
            font-weight: bold;
        }
        
        .card-content {
            margin-bottom: 15px;
        }
        
        .card-price {
            font-size: 1.5rem;
            font-weight: bold;
            color: #ff6b6b;
            margin-bottom: 10px;
        }
        
        .card-description {
            color: #666;
            font-size: 14px;
            line-height: 1.5;
            margin-bottom: 15px;
        }
        
        .ai-explanation {
            background: #f8f9fa;
            padding: 12px;
            border-radius: 8px;
            border-left: 4px solid #667eea;
            font-size: 13px;
            color: #555;
            margin-bottom: 15px;
        }
        
        .card-actions {
            display: flex;
            gap: 10px;
        }
        
        .btn-small {
            padding: 8px 16px;
            font-size: 12px;
        }
        
        .ai-analytics {
            background: rgba(255, 255, 255, 0.95);
            backdrop-filter: blur(20px);
            border-radius: 20px;
            padding: 30px;
            box-shadow: 0 25px 50px rgba(0,0,0,0.15);
        }
        
        .analytics-grid {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
            gap: 20px;
            margin-top: 20px;
        }
        
        .analytics-card {
            background: white;
            border-radius: 12px;
            padding: 20px;
            text-align: center;
            box-shadow: 0 4px 15px rgba(0,0,0,0.1);
        }
        
        .analytics-number {
            font-size: 2.5rem;
            font-weight: bold;
            color: #667eea;
            margin-bottom: 10px;
        }
        
        .analytics-label {
            color: #666;
            font-size: 14px;
        }
        
        .loading {
            text-align: center;
            padding: 40px;
            color: #666;
        }
        
        .loading::after {
            content: '';
            display: inline-block;
            width: 30px;
            height: 30px;
            border: 4px solid #f3f3f3;
            border-top: 4px solid #667eea;
            border-radius: 50%;
            animation: spin 1s linear infinite;
            margin-left: 10px;
        }
        
        @keyframes spin {
            0% { transform: rotate(0deg); }
            100% { transform: rotate(360deg); }
        }
        
        .progress-bar {
            width: 100%;
            height: 8px;
            background: #e9ecef;
            border-radius: 4px;
            overflow: hidden;
            margin: 10px 0;
        }
        
        .progress-fill {
            height: 100%;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            transition: width 0.3s ease;
        }
        
        .algorithm-status {
            display: flex;
            align-items: center;
            gap: 10px;
            margin: 10px 0;
            padding: 10px;
            background: #f8f9fa;
            border-radius: 8px;
        }
        
        .status-indicator {
            width: 12px;
            height: 12px;
            border-radius: 50%;
            background: #28a745;
        }
        
        .status-indicator.warning {
            background: #ffc107;
        }
        
        .status-indicator.error {
            background: #dc3545;
        }
        
        .real-time-updates {
            background: #e8f5e8;
            border: 1px solid #c3e6c3;
            border-radius: 8px;
            padding: 15px;
            margin: 20px 0;
        }
        
        .update-item {
            display: flex;
            justify-content: space-between;
            align-items: center;
            padding: 8px 0;
            border-bottom: 1px solid #d4edda;
        }
        
        .update-item:last-child {
            border-bottom: none;
        }
        
        .update-time {
            color: #666;
            font-size: 12px;
        }
        
        @media (max-width: 768px) {
            .ai-dashboard {
                grid-template-columns: 1fr;
            }
            
            .recommendation-grid {
                grid-template-columns: 1fr;
            }
            
            .analytics-grid {
                grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
            }
        }
    </style>
</head>
<body>
    <div class="container">
        <!-- 头部 -->
        <div class="header">
            <h1>🧠 高级AI推荐系统</h1>
            <p>基于深度学习、协同过滤、情感分析的最先进推荐引擎</p>
        </div>
        
        <!-- AI控制面板 -->
        <div class="ai-dashboard">
            <div class="dashboard-panel">
                <div class="panel-title">
                    <span>🎛️</span>
                    AI控制面板
                </div>
                
                <div class="ai-controls">
                    <div class="control-group">
                        <div class="control-label">推荐算法</div>
                        <select class="control-select" id="algorithmSelect">
                            <option value="hybrid">混合算法 (推荐)</option>
                            <option value="deep_learning">深度学习</option>
                            <option value="collaborative">协同过滤</option>
                            <option value="content">内容过滤</option>
                            <option value="real_time">实时推荐</option>
                        </select>
                    </div>
                    
                    <div class="control-group">
                        <div class="control-label">推荐数量</div>
                        <input type="range" class="control-range" id="recommendationCount" min="5" max="20" value="10">
                        <div style="text-align: center; color: #666; font-size: 14px;">
                            <span id="countValue">10</span> 个推荐
                        </div>
                    </div>
                    
                    <div class="control-group">
                        <div class="control-label">个性化程度</div>
                        <input type="range" class="control-range" id="personalizationLevel" min="0" max="100" value="80">
                        <div style="text-align: center; color: #666; font-size: 14px;">
                            <span id="personalizationValue">80</span>%
                        </div>
                    </div>
                    
                    <div class="control-group">
                        <div class="control-label">实时更新</div>
                        <label style="display: flex; align-items: center; cursor: pointer;">
                            <input type="checkbox" id="realTimeEnabled" checked style="margin-right: 8px;">
                            <span>启用实时推荐</span>
                        </label>
                    </div>
                    
                    <div class="control-group">
                        <div class="control-label">A/B测试</div>
                        <label style="display: flex; align-items: center; cursor: pointer;">
                            <input type="checkbox" id="abTestEnabled" checked style="margin-right: 8px;">
                            <span>启用A/B测试</span>
                        </label>
                    </div>
                    
                    <div class="control-group">
                        <div class="control-label">情感分析</div>
                        <label style="display: flex; align-items: center; cursor: pointer;">
                            <input type="checkbox" id="sentimentAnalysisEnabled" checked style="margin-right: 8px;">
                            <span>启用情感分析</span>
                        </label>
                    </div>
                </div>
                
                <div style="text-align: center; margin-top: 20px;">
                    <button class="btn btn-primary" onclick="generateAdvancedRecommendations()" style="padding: 15px 30px; font-size: 16px;">
                        🚀 生成高级推荐
                    </button>
                    <button class="btn btn-info" onclick="startContinuousLearning()" style="padding: 15px 30px; font-size: 16px;">
                        🧠 启动持续学习
                    </button>
                </div>
            </div>
            
            <div class="dashboard-panel">
                <div class="panel-title">
                    <span>📊</span>
                    AI系统状态
                </div>
                
                <div class="algorithm-status">
                    <div class="status-indicator"></div>
                    <span>深度学习模型: 运行中</span>
                </div>
                
                <div class="algorithm-status">
                    <div class="status-indicator"></div>
                    <span>协同过滤引擎: 运行中</span>
                </div>
                
                <div class="algorithm-status">
                    <div class="status-indicator"></div>
                    <span>内容过滤引擎: 运行中</span>
                </div>
                
                <div class="algorithm-status">
                    <div class="status-indicator"></div>
                    <span>实时推荐引擎: 运行中</span>
                </div>
                
                <div class="algorithm-status">
                    <div class="status-indicator"></div>
                    <span>情感分析器: 运行中</span>
                </div>
                
                <div class="algorithm-status">
                    <div class="status-indicator"></div>
                    <span>A/B测试管理器: 运行中</span>
                </div>
                
                <div style="margin-top: 20px;">
                    <div class="control-label">系统负载</div>
                    <div class="progress-bar">
                        <div class="progress-fill" style="width: 65%;"></div>
                    </div>
                    <div style="text-align: center; color: #666; font-size: 14px;">65%</div>
                </div>
                
                <div style="margin-top: 20px;">
                    <div class="control-label">推荐准确率</div>
                    <div class="progress-bar">
                        <div class="progress-fill" style="width: 94%;"></div>
                    </div>
                    <div style="text-align: center; color: #666; font-size: 14px;">94%</div>
                </div>
            </div>
        </div>
        
        <!-- 推荐结果 -->
        <div class="recommendation-results">
            <div class="panel-title">
                <span>🎯</span>
                高级AI推荐结果
            </div>
            
            <div id="recommendationResults">
                <div style="text-align: center; padding: 40px; color: #666;">
                    <p>点击"生成高级推荐"开始体验最先进的AI推荐系统</p>
                </div>
            </div>
        </div>
        
        <!-- AI分析 -->
        <div class="ai-analytics">
            <div class="panel-title">
                <span>📈</span>
                AI推荐分析
            </div>
            
            <div class="analytics-grid">
                <div class="analytics-card">
                    <div class="analytics-number" id="totalRecommendations">0</div>
                    <div class="analytics-label">总推荐数</div>
                </div>
                
                <div class="analytics-card">
                    <div class="analytics-number" id="clickRate">0%</div>
                    <div class="analytics-label">点击率</div>
                </div>
                
                <div class="analytics-card">
                    <div class="analytics-number" id="conversionRate">0%</div>
                    <div class="analytics-label">转化率</div>
                </div>
                
                <div class="analytics-card">
                    <div class="analytics-number" id="userSatisfaction">0</div>
                    <div class="analytics-label">用户满意度</div>
                </div>
                
                <div class="analytics-card">
                    <div class="analytics-number" id="algorithmAccuracy">0%</div>
                    <div class="analytics-label">算法准确率</div>
                </div>
                
                <div class="analytics-card">
                    <div class="analytics-number" id="realTimeUpdates">0</div>
                    <div class="analytics-label">实时更新次数</div>
                </div>
            </div>
            
            <div class="real-time-updates" id="realTimeUpdatesPanel" style="display: none;">
                <h4 style="margin-bottom: 15px; color: #155724;">🔄 实时更新日志</h4>
                <div id="realTimeUpdatesList">
                    <!-- 实时更新项目将在这里显示 -->
                </div>
            </div>
        </div>
    </div>

    <script src="database.js"></script>
    <script src="advanced-ai-engine.js"></script>
    <script>
        // 页面初始化
        window.onload = function() {
            initializeAdvancedAI();
            setupEventListeners();
            startRealTimeMonitoring();
        };
        
        // 初始化高级AI系统
        function initializeAdvancedAI() {
            console.log('🧠 高级AI推荐系统初始化中...');
            
            // 更新滑块显示
            updateSliderDisplay();
            
            // 初始化AI引擎
            if (window.advancedAIEngine) {
                console.log('✅ 高级AI引擎已加载');
            } else {
                console.error('❌ 高级AI引擎加载失败');
            }
        }
        
        // 设置事件监听器
        function setupEventListeners() {
            // 推荐数量滑块
            document.getElementById('recommendationCount').addEventListener('input', function() {
                document.getElementById('countValue').textContent = this.value;
            });
            
            // 个性化程度滑块
            document.getElementById('personalizationLevel').addEventListener('input', function() {
                document.getElementById('personalizationValue').textContent = this.value;
            });
        }
        
        // 更新滑块显示
        function updateSliderDisplay() {
            const countSlider = document.getElementById('recommendationCount');
            const personalizationSlider = document.getElementById('personalizationLevel');
            
            document.getElementById('countValue').textContent = countSlider.value;
            document.getElementById('personalizationValue').textContent = personalizationSlider.value;
        }
        
        // 生成高级推荐
        async function generateAdvancedRecommendations() {
            const resultsDiv = document.getElementById('recommendationResults');
            resultsDiv.innerHTML = '<div class="loading">🧠 AI正在深度分析中...</div>';
            
            try {
                // 获取用户设置
                const algorithm = document.getElementById('algorithmSelect').value;
                const count = parseInt(document.getElementById('recommendationCount').value);
                const personalization = parseInt(document.getElementById('personalizationLevel').value);
                const realTime = document.getElementById('realTimeEnabled').checked;
                const abTest = document.getElementById('abTestEnabled').checked;
                const sentiment = document.getElementById('sentimentAnalysisEnabled').checked;
                
                // 模拟用户ID（实际应用中从登录状态获取）
                const userId = 1;
                
                // 生成高级推荐
                const result = await window.advancedAIEngine.generateAdvancedRecommendations(userId, {
                    count: count,
                    algorithm: algorithm,
                    realTime: realTime,
                    abTest: abTest,
                    sentimentAnalysis: sentiment,
                    personalizationLevel: personalization / 100
                });
                
                // 显示推荐结果
                displayRecommendationResults(result);
                
                // 更新分析数据
                updateAnalytics(result);
                
                // 记录推荐行为
                recordRecommendationBehavior(userId, result);
                
            } catch (error) {
                console.error('生成高级推荐失败:', error);
                resultsDiv.innerHTML = `
                    <div style="text-align: center; padding: 40px; color: #dc3545;">
                        <p>❌ 推荐生成失败: ${error.message}</p>
                        <button class="btn btn-primary" onclick="generateAdvancedRecommendations()">重试</button>
                    </div>
                `;
            }
        }
        
        // 显示推荐结果
        function displayRecommendationResults(result) {
            const resultsDiv = document.getElementById('recommendationResults');
            
            if (result.recommendations.length === 0) {
                resultsDiv.innerHTML = `
                    <div style="text-align: center; padding: 40px; color: #666;">
                        <p>暂无推荐结果</p>
                    </div>
                `;
                return;
            }
            
            resultsDiv.innerHTML = `
                <div style="margin-bottom: 20px; padding: 20px; background: #f8f9fa; border-radius: 12px;">
                    <h3 style="margin-bottom: 10px;">🎯 推荐摘要</h3>
                    <p><strong>算法:</strong> ${result.algorithm}</p>
                    <p><strong>置信度:</strong> ${(result.confidence * 100).toFixed(1)}%</p>
                    <p><strong>A/B测试组:</strong> ${result.abTestVariant || '无'}</p>
                    <p><strong>推荐数量:</strong> ${result.recommendations.length}</p>
                </div>
                
                <div class="recommendation-grid">
                    ${result.recommendations.map((rec, index) => `
                        <div class="recommendation-card">
                            <div class="card-header">
                                <div class="card-title">${rec.name}</div>
                                <div class="ai-score">AI评分 ${Math.round(rec.score * 100)}</div>
                            </div>
                            <div class="card-content">
                                <div class="card-price">¥${rec.price}</div>
                                <div class="card-description">${rec.description}</div>
                                <div class="ai-explanation">
                                    <strong>AI推荐理由:</strong> ${result.explanation[index] || '基于您的偏好推荐'}
                                </div>
                            </div>
                            <div class="card-actions">
                                <button class="btn btn-primary btn-small" onclick="addToCart(${rec.id})">加入购物车</button>
                                <button class="btn btn-info btn-small" onclick="viewDetails(${rec.id})">查看详情</button>
                            </div>
                        </div>
                    `).join('')}
                </div>
            `;
        }
        
        // 更新分析数据
        function updateAnalytics(result) {
            // 模拟分析数据更新
            const analytics = {
                totalRecommendations: Math.floor(Math.random() * 1000) + 500,
                clickRate: (Math.random() * 20 + 15).toFixed(1),
                conversionRate: (Math.random() * 10 + 5).toFixed(1),
                userSatisfaction: (Math.random() * 2 + 3).toFixed(1),
                algorithmAccuracy: (Math.random() * 10 + 85).toFixed(1),
                realTimeUpdates: Math.floor(Math.random() * 50) + 20
            };
            
            document.getElementById('totalRecommendations').textContent = analytics.totalRecommendations;
            document.getElementById('clickRate').textContent = analytics.clickRate + '%';
            document.getElementById('conversionRate').textContent = analytics.conversionRate + '%';
            document.getElementById('userSatisfaction').textContent = analytics.userSatisfaction;
            document.getElementById('algorithmAccuracy').textContent = analytics.algorithmAccuracy + '%';
            document.getElementById('realTimeUpdates').textContent = analytics.realTimeUpdates;
        }
        
        // 记录推荐行为
        function recordRecommendationBehavior(userId, result) {
            // 记录用户行为用于学习
            if (window.advancedAIEngine) {
                result.recommendations.forEach(rec => {
                    window.advancedAIEngine.realTimeEngine.processEvent({
                        type: 'recommendation',
                        userId: userId,
                        itemId: rec.id,
                        score: rec.score,
                        timestamp: Date.now()
                    });
                });
            }
        }
        
        // 启动持续学习
        function startContinuousLearning() {
            if (window.advancedAIEngine) {
                window.advancedAIEngine.startContinuousLearning();
                showMessage('🧠 持续学习已启动，AI将不断优化推荐效果', 'success');
            } else {
                showMessage('❌ 高级AI引擎未加载', 'error');
            }
        }
        
        // 开始实时监控
        function startRealTimeMonitoring() {
            setInterval(() => {
                updateRealTimeStatus();
            }, 5000); // 每5秒更新一次
        }
        
        // 更新实时状态
        function updateRealTimeStatus() {
            const updatesPanel = document.getElementById('realTimeUpdatesPanel');
            const updatesList = document.getElementById('realTimeUpdatesList');
            
            // 模拟实时更新
            if (Math.random() > 0.7) {
                updatesPanel.style.display = 'block';
                
                const updateItem = document.createElement('div');
                updateItem.className = 'update-item';
                updateItem.innerHTML = `
                    <span>🔄 推荐模型已更新</span>
                    <span class="update-time">${new Date().toLocaleTimeString()}</span>
                `;
                
                updatesList.insertBefore(updateItem, updatesList.firstChild);
                
                // 保持最多10个更新记录
                while (updatesList.children.length > 10) {
                    updatesList.removeChild(updatesList.lastChild);
                }
            }
        }
        
        // 添加到购物车
        function addToCart(itemId) {
            showMessage('✅ 已添加到购物车！', 'success');
            
            // 记录用户行为
            if (window.advancedAIEngine) {
                window.advancedAIEngine.realTimeEngine.processEvent({
                    type: 'click',
                    userId: 1,
                    itemId: itemId,
                    timestamp: Date.now()
                });
            }
        }
        
        // 查看详情
        function viewDetails(itemId) {
            showMessage('🔍 正在加载菜品详情...', 'info');
            
            // 记录用户行为
            if (window.advancedAIEngine) {
                window.advancedAIEngine.realTimeEngine.processEvent({
                    type: 'view',
                    userId: 1,
                    itemId: itemId,
                    timestamp: Date.now()
                });
            }
        }
        
        // 显示消息
        function showMessage(text, type) {
            const messageDiv = document.createElement('div');
            messageDiv.style.cssText = `
                position: fixed;
                top: 20px;
                right: 20px;
                padding: 15px 20px;
                border-radius: 8px;
                color: white;
                font-weight: 500;
                z-index: 1000;
                animation: slideIn 0.3s ease;
            `;
            
            const colors = {
                success: '#28a745',
                error: '#dc3545',
                info: '#17a2b8',
                warning: '#ffc107'
            };
            
            messageDiv.style.background = colors[type] || colors.info;
            messageDiv.textContent = text;
            
            document.body.appendChild(messageDiv);
            
            setTimeout(() => {
                messageDiv.remove();
            }, 3000);
        }
        
        // 添加CSS动画
        const style = document.createElement('style');
        style.textContent = `
            @keyframes slideIn {
                from { transform: translateX(100%); opacity: 0; }
                to { transform: translateX(0); opacity: 1; }
            }
        `;
        document.head.appendChild(style);
    </script>
</body>
</html>
